﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

using System.Diagnostics;
using Microsoft.Office.Server.UserProfiles;

namespace eGov.SharePoint.MyPeeps.WebControls
{
    public partial class MyPeepsLink : System.Web.UI.UserControl
    {
        public bool _error = false;
        private string _pageurl = string.Empty;

        //LinkButton linkBtnMyPeeps = new LinkButton();
        HyperLink hyperlinkBtnMyPeeps = null;

        protected void Page_Load(object sender, EventArgs e)
        {
           

            if (!IsPostBack)
            {
                InitControls();                
            }

        }

        // Override the OnInit method to write text to the 
        
        //[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        //protected override void OnInit(EventArgs e)
        //{
        //    base.OnInit(e);
            
        //}

        private void InitControls()
        {
            Control ctrl = this.FindControl("hyperlinkBtnMyPeeps");
            if (ctrl != null)
            {
                InitURL();

                hyperlinkBtnMyPeeps = (HyperLink)ctrl;
                hyperlinkBtnMyPeeps.NavigateUrl = _pageurl;
            }

            //Create button            

           // linkBtnMyPeeps.PostBackUrl = _pageurl;
           // linkBtnMyPeeps.Enabled = true;


        }


        private void InitURL()
        {
            string currentLoggedUser = string.Empty;

            SPSite siteColl = null;
            SPWeb site = null;

            siteColl = SPContext.Current.Site;
            site = SPContext.Current.Web;

            //SPWebApplication webApp = SPContext.Current.Site.WebApplication;
            //SPSiteCollection siteColls = webApp.Sites;

            string webAppUrl = SPContext.Current.Site.WebApplication.Sites["/"].Url;
            Debug.WriteLine("Web App URL = {0}", webAppUrl);

            try
            {
                SPUser currentuser = site.CurrentUser;
                if (currentuser != null)
                    currentLoggedUser = currentuser.LoginName;

                string[] splittedAccountName = null;

                if (currentuser != null)
                    splittedAccountName = currentLoggedUser.Split('\\');

                string userAcc = string.Empty;
                
                //Split the acount CONTOSO\user
                if (splittedAccountName != null && splittedAccountName.Length > 1)
                    userAcc = splittedAccountName[1];
                if (userAcc.Length == 0)
                {
                    //User does not have a domain
                    userAcc = currentuser.LoginName;
                }
                
                //Set MyPeeps URL
                //string urlString = site.Url;
                //Debug.WriteLine("URL String = {0}", urlString);
                
                UserProfileManager profileManager = new UserProfileManager(SPServiceContext.GetContext(SPContext.Current.Site));
                string myPeepsPage = String.Format("/" + profileManager.PersonalSiteInclusion + "/{0}/MyPeeps.aspx", userAcc.Replace(".", "_"));
                _pageurl = webAppUrl + myPeepsPage;
                Debug.WriteLine("Page URL = {0}", _pageurl);

            }
            catch (Exception exp)
            {

                Debug.WriteLine(exp.ToString());
                HandleException(exp);

            }
        }

        private void HandleException(Exception ex)
        {
            this._error = true;

        }        
    }    
}